import config from "../util/config";
import {h} from "vue";
import {formatDateTime} from "../util/utils";
import {NProgress} from "naive-ui";
import store from "../store";
import {EnumMapStore} from "./Common";

export class PageQueryRunningParam {
    jobName?: string;
    scheduleName?: string;
    scheduleTimeStart?: number;
    scheduleTimeEnd?: number;
}

export class JobRunningVO {
    id?: bigint;
    jobName?: string;
    scheduleName?: string;
    jobClassName?: string;
    jobScheduleId?: bigint;
    applicationName?: string;
    nodeIp?: string;
    scheduleParam?: string;
    scheduleTime?: number;
    normalScheduleTime?: number;
    retryTimes?: number;
    scheduleRatio?: number;
    completeTime?: number;
}

export class ManualTriggerParam {
    jobScheduleId?: bigint;
    nodeId?: bigint;
    param?: string;
}

export const basicColumns = [
    {
        title: "任务名称",
        key: "jobName",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "调度名称",
        key: "scheduleName",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "任务类名",
        key: "jobClassName",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "应用名称",
        key: "applicationName",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "节点IP",
        key: "nodeIp",
        width: 135
    },
    {
        title: "执行参数",
        key: "scheduleParam",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "发起时间",
        key: "scheduleTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobRunningVO) => {
            return h("span", formatDateTime(rowData.scheduleTime));
        }
    },
    {
        title: "正常发起时间",
        key: "normalScheduleTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobRunningVO) => {
            return h("span", formatDateTime(rowData.normalScheduleTime));
        }
    },
    {
        title: "重试次数",
        key: "retryTimes"
    },
    {
        title: "任务进度",
        key: "scheduleRatio",
        width: 200,
        render: (rowData: JobRunningVO) => {
            const ratio = rowData.scheduleRatio as number;
            const scheduleRatioMap = store().enumMapStore.ScheduleRatio;
            const ratioDesc = scheduleRatioMap.get(ratio);
            switch (ratio) {
                case -5:
                    return h(NProgress, {type: "line", percentage: 100, color: "darkorange"}, {default: () => ratioDesc});
                case -4:
                    return h(NProgress, {type: "line", percentage: 100, color: "goldenrod"}, {default: () => ratioDesc});
                case -3:
                    return h(NProgress, {type: "line", percentage: 100, status: "warning"}, {default: () => ratioDesc});
                case -2:
                    return h(NProgress, {type: "line", percentage: 100, color: "red"}, {default: () => ratioDesc});
                case -1:
                    return h(NProgress, {type: "line", percentage: 100, status: "error"}, {default: () => ratioDesc});
                case 0:
                    return h(NProgress, {type: "line", percentage: 0, status: "info"}, {default: () => ratioDesc});
                case 1:
                    return h(NProgress, {type: "line", percentage: 1, status: "success"}, {default: () => ratioDesc});
                case 100:
                    return h(NProgress, {type: "line", "indicator-placement": "inside", percentage: 100, status: "success"}, {default: () => ratioDesc});
                default:
                    return h(NProgress, {type: "line", "indicator-placement": "inside", percentage: rowData.scheduleRatio, processing: true});
            }

        }
    },
    {
        title: "结束时间",
        key: "completeTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobRunningVO) => {
            if (!rowData.completeTime) {
                return "-";
            }
            return h("span", formatDateTime(rowData.completeTime));
        }
    }
];
